from visual import *

from visual import *
import time
import sys


def Lboundary(x, lx):
    res=x
    while x>=lx:
        x-=lx
    while x<0:
        x+=lx
    return x

def boundary(pos, lx,ly,lz):
    pos[0]=Lboundary(pos[0],lx)
    pos[1]=Lboundary(pos[1],ly)
    #pos[2]=Lboundary(pos[2],lz)
    return pos

def showSystem(xpos,diameter):
    sistema=[]
    scene.background = color.gray(0.7)
    for i in range(len(diameter)):
        ball = sphere (pos=xpos[i], radius=diameter[i]*0.5, color = color.red ,material=materials.rough)
        sistema.append(ball)
    return sistema

def printBox(lbox,rad):
    display(center=(lbox[0]*0.5,lbox[1]*0.5,lbox[2]*0.5))
    lx=lbox[0]
    ly=lbox[1]
    lz=lbox[2]
    rod = cylinder(pos=(0,0,0),axis=(lx,0,0), radius=rad)
    rod = cylinder(pos=(0,0,0),axis=(0,ly,0), radius=rad)
    rod = cylinder(pos=(0,0,0),axis=(0,0,lz), radius=rad)
    rod = cylinder(pos=(lx,0,0),axis=(0,ly,0), radius=rad)
    rod = cylinder(pos=(lx,0,0),axis=(0,0,lz), radius=rad)
    rod = cylinder(pos=(0,ly,0),axis=(0,0,lz), radius=rad)
    rod = cylinder(pos=(0,ly,0),axis=(lx,0,0), radius=rad)
    
    rod = cylinder(pos=(0,0,lz),axis=(lx,0,0), radius=rad)
    rod = cylinder(pos=(0,0,lz),axis=(0,ly,0), radius=rad)
    
    rod = cylinder(pos=(lx,ly,0),axis=(0,0,lz), radius=rad)
    rod = cylinder(pos=(lx,0,lz),axis=(0,ly,0), radius=rad)
    rod = cylinder(pos=(0,ly,lz),axis=(lx,0,0), radius=rad)

def importConfig(name):
    f = open(name,'r')
    tx = f.readline()
    tx = tx.replace('\n','')
    val = tx.split(' ')
    aux=[]
    for v in val:
        if v!='':
            aux.append(v)
    t=float(aux[1])
    tcorr=float(aux[3])
    icorr=int(aux[5])
    npart=int(aux[7])
    lx=float(aux[9])
    ly=float(aux[10])
    lz=float(aux[11])
    printBox([lx,ly,lz],0.1)
    tx = f.readline()
    tx = tx.replace('\n','')
    val = tx.split(' ')
    aux=[]
    for v in val:
        if v!='':
            aux.append(v)
    sigma=[float(aux[1]),float(aux[2]),float(aux[3]),float(aux[4]),float(aux[5]),float(aux[6])]
    pos=[]
    vel=[]
    force=[]
    diameter=[]
    
    for i in range(npart):
        tx = f.readline()
        tx = tx.replace('\n','')
        val = tx.split(' ')
        aux=[]
        for v in val:
            if v!='':
                aux.append(v)
    
        xpos=boundary([float(aux[0]),float(aux[1]),float(aux[2])], lx,ly,lz)
        pos.append(xpos)
        vel.append([float(aux[3]),float(aux[4]),float(aux[5])])
        force.append([float(aux[6]),float(aux[7]),float(aux[8])])
        diameter.append(float(aux[9]))
    return [pos,vel,force,diameter]
    

    
name = '/home/jonk/eclipse/analisis/salida.dat'
[pos,vel,force,diameter] = importConfig(name)
sistema = showSystem(pos,diameter)


